import React, { useState } from "react";
import { Form, message } from "antd";
import { IPermission } from "@/api/interface/permission";
import { getAllPermissionList } from "@/api/modules/permission";
import { getAuthMenuList } from "@/api/modules/menu";
import { findRolePermissions, grantPermissionsToRole } from "@/api/modules/rolePermission";
import { ResultEnum } from "@/enums/httpEnum";

const useGrantPermissionsToRole = () => {
	const [isGrantPermissionModalOpen, setGrantPermissionModalOpen] = useState(false);
	const [grantPermissionForm] = Form.useForm();
	const [grantPermissionSubmitting, setSrantPermissionSubmitting] = useState(false);
	const [roleId, setRoleId] = useState<number | null>(null);
	const [permissionList, setPermissionList] = useState<IPermission[]>([]);
	const [permissionIds, setPermissionIds] = useState<React.Key[]>([]);
	const [menuIds, setMenuIds] = useState<React.Key[]>([]);
	const [menuList, setMenuList] = useState<Menu.MenuOptions[]>([]);
	const [initPermissionIds, setInitPermissionIds] = useState<React.Key[]>([]);
	const [initMenuIds, setInitMenuIds] = useState<React.Key[]>([]);

	const getPermissions = async () => {
		const { code, data } = await getAllPermissionList();
		if (code === ResultEnum.SUCCESS && data) {
			setPermissionList(data);
		}
		console.log(permissionList);
		// setPermissionList(res);
	};

	const getMenus = async () => {
		const { code, data } = await getAuthMenuList();
		if (code === 200) {
			setMenuList(data || []);
		}
	};

	const GrantPermissionsModalOpen = async (roleId: number) => {
		setRoleId(roleId);
		getPermissions();
		getMenus();

		const { code, data } = await findRolePermissions(roleId);
		if (code === 200) {
			const permissionIdList = data.permissions?.map((item: any) => item.id);
			const menuIdList = data.menus?.map((item: any) => item.id);
			setPermissionIds(permissionIdList);
			setMenuIds(menuIdList);
			setInitPermissionIds(permissionIdList);
			setInitMenuIds(menuIdList);
		}

		setGrantPermissionModalOpen(true);
	};

	const handleGrantPermissionsCancel = () => {
		setRoleId(null);
		setGrantPermissionModalOpen(false);
	};

	const handleGrantPermissionsSubmit = async () => {
		try {
			setSrantPermissionSubmitting(true);

			const { code, msg } = await grantPermissionsToRole(roleId, permissionIds, menuIds);
			if (code === 200) {
				message.success(msg);
			}
			handleGrantPermissionsCancel();
			setSrantPermissionSubmitting(false);
		} catch (err) {
			setSrantPermissionSubmitting(false);
			console.log(err);
		}
	};

	const onSelectPermissionChange = (selectedKeysValue: React.Key[]) => {
		setPermissionIds([...selectedKeysValue]);
	};

	const menuOnchange = (selectedKeysValue: React.Key[]) => {
		setMenuIds([...selectedKeysValue]);
	};

	return {
		grantPermissionForm,
		isGrantPermissionModalOpen,
		setGrantPermissionModalOpen,
		grantPermissionSubmitting,
		GrantPermissionsModalOpen,
		handleGrantPermissionsSubmit,
		handleGrantPermissionsCancel,
		menuList,
		menuIds,
		permissionList,
		permissionIds,
		initPermissionIds,
		initMenuIds,
		onSelectPermissionChange,
		menuOnchange
	};
};

export default useGrantPermissionsToRole;
